package com.fjzx.androidframe.base.adapter;

import android.arch.paging.DataSource;
import android.arch.paging.PagedListAdapter;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fjzx.androidframe.base.annotation.injector.ItemViewInjector;
import com.fjzx.androidframe.base.callback.OnItemClickListener;

/**
 * 基础RecyclerView 分页适配器
 * 接收数据类型为PagedList<T></>
 * 更新数据方法为submitList(PagedList<T>)
 *
 * @param <T>
 * @author xiongyongcong
 */
public abstract class BasePagedRecyclerViewAdapter<T> extends PagedListAdapter<T, BasePagedRecyclerViewAdapter.BasePagedViewHolder> {
    protected LayoutInflater mInflater;
    protected OnItemClickListener mClickListener;

    public BasePagedRecyclerViewAdapter() {
        super(new DiffUtil.ItemCallback<T>() {
            @Override
            public boolean areItemsTheSame(T oldItem, T newItem) {
                return oldItem.equals(newItem);
            }

            @Override
            public boolean areContentsTheSame(T oldItem, T newItem) {
                return oldItem.equals(newItem);
            }
        });
    }

    @NonNull
    @Override
    public BasePagedRecyclerViewAdapter.BasePagedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(parent.getContext());
        }
        View itemView = createItemView(parent, viewType);
        return new BasePagedViewHolder(itemView);
    }

    protected View createItemView(@NonNull ViewGroup parent, int viewType) {
        return mInflater.inflate(ItemViewInjector.inject(this), parent, false);
    }

    @Override
    public void onBindViewHolder(@NonNull BasePagedRecyclerViewAdapter.BasePagedViewHolder holder, int position) {
        bindData(holder, position, getItem(position));
    }

    @Nullable
    @Override
    public T getItem(int position) {
        return super.getItem(position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mClickListener = listener;
    }

    public void refresh() {
        if (getCurrentList() != null) {
            DataSource<?, T> dataSource = getCurrentList().getDataSource();
            if (dataSource != null) {
                dataSource.invalidate();
            }
        }
    }

    public void clear() {
        getCurrentList().clear();
    }

    /**
     * 子类重写此方法，绑定数据 例如： protected abstract void bindData(ViewHolder holder, int
     * position, String data) {
     * ((TextView)holer.getView(R.id.tv_text)).setText(data); }
     *
     * @param holder
     * @param position
     * @param data
     */
    protected abstract void bindData(BasePagedViewHolder holder, int position, @NonNull T data);

    public class BasePagedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public SparseArray<View> mViews = new SparseArray<>();

        public BasePagedViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
        }

        public <T extends View> T getView(int id) {
            View view = mViews.get(id);
            if (view == null) {
                view = itemView.findViewById(id);
                mViews.put(id, view);
            }
            return (T) view;
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) {
                mClickListener.onItemClick(view, getLayoutPosition());
            }
        }
    }
}
